
//
//  YYENotesTableViewCell.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/13.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYENotesTableViewCell.h"
#import "EnlargedImage.h"


@interface YYENotesTableViewCell ()

@property (nonatomic,strong) UIImageView *headerIV;
@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong) UILabel *timeLabel;
@property (nonatomic,strong) UILabel *contentLabel;
@property (nonatomic,strong) UIImageView *contentIV;

@end

@implementation YYENotesTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.headerIV = [[UIImageView alloc]init];
        self.headerIV.layer.cornerRadius = 19;
        self.headerIV.layer.masksToBounds = YES;
        [self.contentView addSubview:self.headerIV];
        
        self.nameLabel = [[UILabel alloc]init];
        self.nameLabel.textColor = [UIColor blackColor];
        self.nameLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.nameLabel];
        
        self.timeLabel = [[UILabel alloc]init];
        self.timeLabel.textColor = LineColor;
        self.timeLabel.font = [UIFont systemFontOfSize:11];
        [self.contentView addSubview:self.timeLabel];
        
        self.deleteBtn = [[UIButton alloc]init];
        [self.deleteBtn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
        [self.deleteBtn addTarget:self action:@selector(deleteClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.deleteBtn];
        
//        self.noteView = [[UIView alloc]init];
//        [self.contentView addSubview:self.noteView];
        
        self.contentLabel = [[UILabel alloc]init];
        self.contentLabel.textColor = [UIColor blackColor];
        self.contentLabel.font = [UIFont systemFontOfSize:14];
        self.contentLabel.numberOfLines = 0;
        [self.contentView addSubview:self.contentLabel];
        
        self.contentIV = [[UIImageView alloc]init];
        self.contentIV.contentMode = UIViewContentModeScaleAspectFill;
        self.contentIV.clipsToBounds = YES;
        [self.contentView addSubview:self.contentIV];
        
        self.contentIV.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgAction:)];
        [self.contentIV addGestureRecognizer:tap];
        
        self.lineView = [[UIView alloc]init];
        self.lineView.backgroundColor = LightLineColor;
        [self.contentView addSubview:self.lineView];
        
        self.commentBtn = [[YSLCustomButton alloc]init];
        [self.commentBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.commentBtn setImage:[UIImage imageNamed:@"comment"] forState:UIControlStateNormal];
        self.commentBtn.ysl_buttonType = YSLCustomButtonImageLeft;
        self.commentBtn.ysl_spacing = 3;
        [self.commentBtn addTarget:self action:@selector(commentClicked) forControlEvents:UIControlEventTouchUpInside];
        self.commentBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.commentBtn];
        
        self.praiseBtn = [[YSLCustomButton alloc]init];
        [self.praiseBtn setImage:[UIImage imageNamed:@"unpraise"] forState:UIControlStateNormal];
        [self.praiseBtn setImage:[UIImage imageNamed:@"praise"] forState:UIControlStateSelected];
        self.praiseBtn.ysl_buttonType = YSLCustomButtonImageLeft;
        self.praiseBtn.ysl_spacing = 3;
        [self.praiseBtn addTarget:self action:@selector(praiseClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.praiseBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.praiseBtn setTitleColor:GeneralDarkRed forState:UIControlStateSelected];
        self.praiseBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.praiseBtn];
        
        self.bottomView = [[UIView alloc]init];
        self.bottomView.backgroundColor = LightLineColor;
        [self.contentView addSubview:self.bottomView];
        
        //praise    unpraise
        
        self.nameLabel.text = @"陈晨";
        self.timeLabel.text = @"3分钟前";
        self.contentLabel.text = @"怎么可以英语轻松过四级？今天我整理出了一些好的词汇";
        
        
    }
    return self;
    
}

- (void)imgAction:(UITapGestureRecognizer *)tap
{
    UIImageView *imgView = (UIImageView *)tap.view;
    
    [EnlargedImage enlargedImage:imgView enlargedTime:0.5]; //使用

}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.headerIV makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.left).offset(17);
        make.top.equalTo(self.contentView.top).offset(10);
        make.size.equalTo(CGSizeMake(38, 38));
    }];
    
    [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerIV.right).offset(14);
        make.bottom.equalTo(self.headerIV.centerY);
        make.right.equalTo(self.contentView.right).offset(-17);
    }];
    
    [self.timeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerIV.right).offset(14);
        make.top.equalTo(self.headerIV.centerY);
    }];
    
//    [self.noteView makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.equalTo(self.contentView);
//        make.top.equalTo(self.headerIV.bottom).offset(10);
//        make.bottom.equalTo(self.contentView.bottom).offset(-40);
//    }];
    
    [self.deleteBtn makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.right).offset(-15);
        make.centerY.equalTo(self.nameLabel.centerY);
        make.size.equalTo(CGSizeMake(24, 12));
    }];
    
    [self.contentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.left).offset(17);
        make.right.equalTo(self.contentView.right).offset(-17);
        make.top.equalTo(self.headerIV.bottom).offset(10);
    }];
    
    [self.contentIV makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.left).offset(17);
        make.top.equalTo(self.contentLabel.bottom).offset(8);
        make.size.equalTo(CGSizeMake(157, 195));
    }];
    
    [self.lineView makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.height.equalTo(0.5);
        make.bottom.equalTo(self.contentView.bottom).offset(-40);
    }];
    
    [self.commentBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.left);
        make.bottom.equalTo(self.contentView.bottom).offset(-5);
        make.size.equalTo(CGSizeMake(SCREEN_WIDTH / 2, 35));
    }];
    
    [self.praiseBtn makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.right);
        make.bottom.equalTo(self.contentView.bottom).offset(-5);
        make.size.equalTo(CGSizeMake(SCREEN_WIDTH / 2, 35));
    }];
    
    [self.bottomView makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.contentView);
        make.height.equalTo(5);
    }];
    
}

- (void)setModel:(YYENotesModel *)model {
    _model = model;
    
    [self.headerIV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", model.img]] placeholderImage:nil];
    self.nameLabel.text = model.name;
    self.timeLabel.text = model.created_at;
    [self.contentIV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", model.image]] placeholderImage:[UIImage imageNamed:@"courseHolder"]];
    self.contentLabel.text = model.note;
    [self.praiseBtn setTitle:model.like_count forState:UIControlStateNormal];
    [self.commentBtn setTitle:model.comment_count forState:UIControlStateNormal];

    
    NSString *uid = [LoginModel shareLogin].uid;
    if (model.uid.integerValue == uid.integerValue) {
        self.deleteBtn.hidden = NO;
    }else {
        self.deleteBtn.hidden = YES;
    }
    
    if ([self.model.is_liked isEqualToString:@"0"]) {
//        [self.praiseBtn setImage:[UIImage imageNamed:@"unpraise"] forState:UIControlStateNormal];
        [self.praiseBtn setSelected:NO];
    }else if ([self.model.is_liked isEqualToString:@"1"]) {
//        [self.praiseBtn setImage:[UIImage imageNamed:@"praise"] forState:UIControlStateNormal];
        [self.praiseBtn setSelected:YES];
    }
    
//    if (model.comment_count.integerValue > 0) {
//        [self.commentBtn setTitle:model.comment_count forState:UIControlStateNormal];
//    }else {
//        [self.commentBtn setTitle:@"" forState:UIControlStateNormal];
//    }
    if (model.image.length > 0) {
        self.contentIV.hidden = NO;
    }else {
        self.contentIV.hidden = YES;
    }
    
}

// 评论
- (void)commentClicked {
    
    if ([self.delegate respondsToSelector:@selector(commentsWithCell:)]) {
        [self.delegate commentsWithCell:self];
    }
    
}

// 点赞
- (void)praiseClicked {
    
    if ([self.model.is_liked isEqualToString:@"0"]) {
//        [self.praiseBtn setImage:[UIImage imageNamed:@"praise"] forState:UIControlStateNormal];
        [self.praiseBtn setSelected:YES];
        NSInteger prise = self.model.like_count.integerValue + 1;
        self.model.like_count = @(prise).stringValue;
        [self.praiseBtn setTitle:@(prise).stringValue forState:UIControlStateNormal];
        self.model.is_liked = @"1";
    }else if ([self.model.is_liked isEqualToString:@"1"]) {
//        [self.praiseBtn setImage:[UIImage imageNamed:@"unpraise"] forState:UIControlStateNormal];
        [self.praiseBtn setSelected:NO];
        NSInteger prise = self.model.like_count.integerValue - 1;
        self.model.like_count = @(prise).stringValue;
        [self.praiseBtn setTitle:@(prise).stringValue forState:UIControlStateNormal];

        self.model.is_liked = @"0";
    }
    
    if ([self.delegate respondsToSelector:@selector(commentsWithCell:)]) {
        [self.delegate priseWithCell:self];
    }
    
}

// 删除
- (void)deleteClicked {
    
    if ([self.delegate respondsToSelector:@selector(deleteNoteWithCell:)]) {
        [self.delegate deleteNoteWithCell:self];
    }
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
